package com.waiting.leetcode;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/22
 **/
public class BinarySearch704 {
    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int target = 2;
        BinarySearch704 test = new BinarySearch704();
        System.out.println(test.search(nums, target));
    }

    public int search(int[] nums, int target) {
        if (nums == null || nums.length < 1) {
            return -1;
        }
        return ite(nums, target, 0, nums.length - 1);
    }

    public int ite(int[] nums, int target, int start, int end) {
        if (end < start) {
            return -1;
        }
        int mid = (start + end) / 2;
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] < target) {
            return ite(nums, target, mid + 1, end);
        } else {
            return ite(nums, target, start, mid - 1);
        }
    }
}
